package hot100;

public class IsSymmetric {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public boolean isSymmetric(TreeNode root) {
        return isSymmetricSame(root.left,root.right);
    }

    private boolean isSymmetricSame(TreeNode p, TreeNode q) {
        //左右都为空，那么是对称的
        if (p == null && q == null) return true;

        //一个为空，一个不为空，那么不对称
        if (p == null && q != null) return false;

        if (p != null && q == null) return false;

        //都不为空，那么判断值是否相等，并且继续判断子孙节点是否对称
        return (p.val == q.val) && isSymmetricSame(p.left,q.right) && isSymmetricSame(p.right,q.left);
    }
}
